﻿{include file='pub/header'/}
<title>商品列表</title>
</head>
<body class="pos-r">
<div>
    {if !isset($is_show)}
    <nav class="breadcrumb">{$breadNav}</nav>
    {/if}
    <div class="page-container">
        <div class="text-c">
            <form action="" method="get">
                商品名称：<input type="text" name="name" id="" placeholder=" 商品名称" style="width:250px" class="input-text">
                商品货号：<input type="text" name="gno" id="" placeholder=" 商品货号" style="width:250px" class="input-text">
                <button class="btn btn-success" type="submit"><i class="Hui-iconfont">&#xe665;</i> 搜商品
                </button>
            </form>
        </div>
        <div class="cl pd-5 bg-1 bk-gray mt-20">
            <span class="l">
                <a href="javascript:;" onclick="datadel()" class="btn btn-danger radius">
                    <i class="Hui-iconfont">&#xe6e2;</i> 批量删除
                </a>
                <a class="btn btn-primary radius" onclick="asyncGoods()" href="javascript:;">
                    <i class="Hui-iconfont">&#xe641;</i> 同步商品库
                </a>
                <a class="btn btn-primary radius" onclick="product_add('添加商品','productAdd.html')" href="javascript:;">
                    <i class="Hui-iconfont">&#xe600;</i> 添加商品
                </a>
                <a class="btn btn-primary radius" onclick="updateImage()" href="javascript:;">
                    <i class="Hui-iconfont Hui-iconfont-huanyipi"></i> 更新商品图片
                </a>
                {if isset($is_bulk)}
                <a class="btn btn-primary radius" onclick="addProps()" href="javascript:;">
                    <i class="Hui-iconfont">&#xe600;</i> 条码添加商品规格
                </a>
                {/if}

                {if isset($is_com)}
                <a class="btn btn-primary radius" onclick="downGoods()" href="javascript:;">
                    <i class="Hui-iconfont Hui-iconfont-down"></i> 批量下架
                </a>
                <a class="btn btn-primary radius" onclick="scanUp()" href="javascript:;">
                    <i class="Hui-iconfont  Hui-iconfont-upload"></i> 扫码上架商品
                </a>
                {/if}
            </span>
            <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
               href="javascript:location.replace(location.href);" title="刷新">
                <i class="Hui-iconfont">&#xe68f;</i>
            </a>
        </div>
        <div class="mt-20">
            <table class="table table-border table-bordered table-bg table-hover table-sort">
                <thead>
                <tr class="text-c">
                    <th width="40"><input name="" type="checkbox" value=""></th>
                    <th width="40">ID</th>
                    <th width="40">商品货号</th>
                    <th width="60">缩略图</th>
                    <th width="100">产品名称</th>
                    <th width="100">产品分类</th>
                    <th width="100">单价</th>
                    <th width="100">销售单价</th>
                    <th width="60">单位</th>
                    {if isset($is_com)}
                    <th width="100">商品库存</th>
                    {/if}
                    <th width="100">商品排序</th>
                    <th width="60">是否首页推荐</th>
                    <!--<th width="60">是否为积分商品</th>-->
                    <th width="60">商品所属类别</th>
                    {if isset($is_bulk)}
                    <th width="60">商品规格管理</th>
                    {/if}
                    <th width="100">操作</th>
                </tr>
                </thead>
                <tbody>
                {foreach $goodsList as $good}
                <tr class="text-c va-m">
                    <td><input name="id" type="checkbox" value="{$good.id}"></td>
                    <td>{$good.id}</td>
                    <td>{$good.gno}</td>
                    <td>
                        <a onClick="picture_show('查看图片', '{$good.img}')" data-src="" href="javascript:;">
                            <img width="60" class="product-thumb lazy" data-original="{:explode(',', $good['img'])[0]}">
                        </a>
                    </td>
                    <td>
                        {$good.name}
                    </td>
                    <td>
                        {$cat_list[$good.cate_id]}
                    </td>
                    <td>
                        {$good.sale_price}
                    </td>
                    <td>
                        {$good.active_price}
                    </td>
                    <td>
                        {$good.goodattr}
                    </td>
                    {if isset($is_com)}
                    <td>
                        <span>
                        <input type="text" value="{$good.count}" class="input-text" style="width: 60px;"/>
                        <button type="button" class="radius btn-primary btn" onclick="saveRem(this, '{$good.id}')">保存</button>
                        </span>
                    </td>
                    {/if}
                    <td>
                        <span>
                        <input type="text" value="{$good.good_order}" class="input-text" style="width: 60px;"/>
                        <button type="button" class="radius btn-primary btn" onclick="saveOrd(this, '{$good.id}')">保存</button>
                        </span>
                    </td>
                    <td class="is_recommend">
                        {if $good.is_recommend == 1}
                        是
                        {else/}
                        否
                        {/if}
                    </td>
                    <!--<td>-->
                        <!--<a href="javascript:;" onclick="changeScore(this, '{$good.is_score}')" class="btn radius btn-primary">-->
                            <!--{if $good.is_score == 1}-->
                            <!--是-->
                            <!--{else/}-->
                            <!--否-->
                            <!--{/if}-->
                        <!--</a>-->
                    <!--</td>-->
                    <td>
                        {$active_list[$good.active_id]}
                    </td>
                    {if isset($is_bulk)}
                    <td>
                        <a href="javascript:;" class="btn btn-primary radius" onclick="getProps('{$good.id}')">规格管理</a>
                    </td>
                    {/if}
                    <td class="td-manage">
                        <a style="text-decoration:none" class="ml-5"
                           onClick="set_active('{$good.id}', this)"
                           href="javascript:;" title="商品所属类别">
                            商品所属类别
                        </a>
                        <a style="text-decoration:none" class="ml-5"
                           onClick="set_recommend('{$good.id}', this)"
                           href="javascript:;" title="设置推荐">
                            {if $good.is_recommend == 1}
                            取消首页推荐
                            {else/}
                            设置首页推荐
                            {/if}
                        </a>
                        <a style="text-decoration:none" class="ml-5"
                           onClick="changeStatus('{$good.id}', this)"
                           href="javascript:;" title="状态">
                            {if $good.is_live == 1}
                            下架
                            {else/}
                            上架
                            {/if}
                        </a>
                        <a style="text-decoration:none" class="ml-5"
                           onClick="product_edit('产品编辑','{:url(\'Products/productEdit\', [\'good_id\'=>$good.id])}')"
                           href="javascript:;" title="编辑">
                            <i class="Hui-iconfont">&#xe6df;</i>编辑
                        </a>
                        <a style="text-decoration:none" class="ml-5" onClick="product_del(this,'{$good.id}')"
                           href="javascript:;" title="删除">
                            <i class="Hui-iconfont">&#xe6e2;</i>删除
                        </a>
                    </td>
                </tr>
                {/foreach}
                </tbody>
            </table>
        </div>
    </div>
</div>

{include file='pub/footer'/}
<script src="__STATIC__/lib/jquery/jquery.lazyload.js"></script>
<script type="text/javascript">
    // $('.table-sort').dataTable({
    //     "aaSorting": [[1, "asc"]],//默认第几个排序
    //     "bStateSave": false,//状态保存
    //     "aoColumnDefs": [
    //         {"orderable": false, "aTargets": [0, 11]}// 制定列不参与排序
    //     ]
    // });
    /**
     * 获取商品规格
     */
    function getProps(gid){
        layer_show("商品规格", "{:url('Products/getProps')}?gid="+gid, "600", "800");
    }

    $(function () {
        $("img.lazy").lazyload({threshold :180});
    })

    function saveOrd(o, good_id){
        var num = $(o).prev('input').val();
        $.post('{:url("setOrder")}', {id:good_id, num:num},function (res) {
            if(res.code == 1){
                layer.msg('操作成功');
            }
            setTimeout(function () {
                //location.reload()
            }, 888);
        })
    }
    
    function saveRem(o, good_id) {
        var num = $(o).prev('input').val();
        if(num<0){
            layer.msg('库存数量不合法');
        }
        $.post('{:url("set_combine_num")}', {id:good_id, num:num},function (res) {
            if(res.code == 1){
                layer.msg('操作成功');
            }
            setTimeout(function () {
                //location.reload()
            }, 888);
        })
    }
    // 设置商品活动类型
    function set_active(good_id){
        layer_show('活动类型', '{:url("Products/active")}?good_id='+good_id, '300', '180');
    }
    /**
     * 更新商品图片
     */
    function updateImage(){
        layer.load();
        $.post("{:url('updateImage')}", null, function (res) {
            layer.closeAll();
            if(res.code == 1){
                layer.alert('更新成功');
            }else{
                layer.alert(res.msg);
            }
        });
    }
    /**
     * 设置推荐商品
     */
    function set_recommend(good_id, o){
        var status = $.trim($(o).html());
        if(status == '设置首页推荐'){
            var code = 1;
        }else{
            var code = 0;
        }
        $.post("{:url('Products/set_recommend')}", {code:code, good_id:good_id}, function (res) {
            if(res.code == 1){
                if(code == 1){
                    $(o).parent().siblings('.is_recommend').html('是');
                    $(o).html('取消首页推荐');
                    layer.msg('修改成功');
                }else{
                    $(o).parent().siblings('.is_recommend').html('否');
                    $(o).html('设置首页推荐');
                    layer.msg('修改111');
                }
            }else{
                layer.msg('修改失败');
            }
        });
    }
    /**
     * 上下架商品
     */
    function changeStatus(good_id, o){
        var status = $.trim($(o).html());
        if(status == '上架'){
            var code = 1;
        }else{
            var code = 0;
        }
        $.post("{:url('Products/changeStatus')}", {code:code, good_id:good_id}, function (res) {
            if(res.code == 1){
                if(code == 1){
                    $(o).html('下架');
                }else{
                    $(o).html('上架');
                }
                layer.msg('修改成功');
            }else{
                layer.msg(res.msg);
            }
        });
    }

    //同步商品
    function asyncGoods() {
        layer.confirm("确定同步商品库？", function (index) {
            var li = layer.load();
            $.post("{:url('asyncGoods')}", null, function (res) {
                if (res.code == 1) {
                    layer.alert(res.msg, function () {
                        location.reload();
                    });
                } else {
                    layer.close(li);
                    layer.alert(res.msg);
                }
            });
        });
    }

    /*产品-添加*/
    function product_add(title, url) {
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }


    /*图片-查看*/
    function picture_show(title, url, id) {
        var imgsurl = url.split(',');
        var content = '';
        $.each(imgsurl, function (i, v) {
            content += "<img style='width: 120px; margin: 10px;' src='" + v + "'/>";
        });
        var index = layer.open({
            type: 1,
            title: title,
            content: content
        });
        layer.full(index);
    }

    /*产品-编辑*/
    function product_edit(title, url, id) {
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }

    /*产品-删除*/
    function product_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.post("{:url('Products/delData')}", {idstr:id}, function (res) {
                if(res.code == 1){
                    layer.msg('删除成功');
                    $(obj).parents('tr').remove();
                }else{
                    layer.msg(res.msg);
                }
            });
        });
    }
    //删除选中产品
    function datadel() {
        var $ids = $("input[name='id']:checked");
        if($ids.length<1){
            layer.alert('请选中后在操作');
            return false;
        }
        var idarr = new Array();
        $.each($ids, function (i, v) {
            idarr.push($(v).val());
        });
        var idStr = idarr.toString();
        layer.confirm('确认删除选中项？', function () {
            $.post("{:url('Products/delData')}", {idstr:idStr}, function (res) {
                if(res.code == 1){
                    layer.msg('删除成功');
                    $ids.parents('tr').remove();
                }else{
                    layer.msg(res.msg);
                }
            });
        });
    }

    function addProps(){
        layer.open({
            title: '添加规格',
            type: 1,
            area: ['600px','450px'],
            content: '<div style="margin-top: 25px; text-align: center; width: 100%; height: 95%;">' +
            '<textarea id="props" style="width: 80%; height: 75%;" autofocus="autofocus"></textarea>' +
            '<div style="text-align: right; width: 100%; margin-top: 25px;">' +
            '<span style="font-size: 13px; color:red; margin-right: 15px;">多个规格条码以回车分隔开录入</span>' +
            '<button onclick="sureProp()" class="btn btn-primary radius">添加</button></div></div>'
        })
    }

    function sureProp(){
        var props = $.trim($("#props").val());
        if("" == props){
            layer.msg('条码为空');
            return false;
        }else{
            layer.confirm('确定添加规格条码为'+props+"?", function(){
                layer.load();
                $.post("{:url('Products/addProps')}", {props:props}, function(res){
                    if(res.code == -1){
                        layer.alert('添加失败', function(){
                            layer.closeAll();
                        });
                    }else{
                        layer.alert(res.msg, function(){
                            layer.closeAll();
                            location.reload();
                        });
                    }
                }, 'json');
            });
        }
    }

    function downGoods(){
            layer.confirm('确定下架商品？', function() {
                layer.load();
                $.post("{:url('Products/downGoods')}", null, function (res) {
                    if (res.code == 1) {
                        layer.closeAll();
                        layer.alert('下架成功', function () {
                            location.reload();
                        });
                    }
                }, 'json');
            });
    }

    function scanUp(){
        layer.open({
            title: '上架商品',
            type: 1,
            area: ['600px','450px'],
            content: '<div style="margin-top: 25px; text-align: center; width: 100%; height: 95%;">' +
            '<textarea id="props" style="width: 80%; height: 75%;" autofocus="autofocus"></textarea>' +
            '<div style="text-align: right; width: 100%; margin-top: 25px;">' +
            '<span style="font-size: 13px; color:red; margin-right: 15px;">多个条码以回车分隔开录入</span>' +
            '<button onclick="scanLoad()" class="btn btn-primary radius">添加</button></div></div>'
        })
    }

    function scanLoad(){
        var props = $.trim($("#props").val());
        if("" == props){
            layer.msg('条码为空');
            return false;
        }else{
            layer.confirm('确定添加条码为'+props+"?", function(){
                layer.load();
                $.post("{:url('Products/scanLoad')}", {props:props}, function(res){
                    layer.alert(res.msg, function(){
                        layer.closeAll();
                        location.reload();
                    });
                }, 'json');
            });
        }
    }
</script>
</body>
</html>